1472C - Long Jumps - CodeForces Solution


dp graphs *1100

Please click on ads to support us..

Python Code:

def solve():
    l = int(input())
    lst = list(map(int,input().strip().split()))[:l]

    dp = [0 for i in range(l)]
    for i in range(l - 1, -1, -1):
        dp[i] = lst[i]
        j = i + lst[i]
        if j < l:
            dp[i] += dp[j]

    return max(dp)

if __name__ == '__main__':
    n = int(input())

    for i in range(n):
        print(solve())

C++ Code:

#include<iostream>
using namespace std;
long long a[200010],n,maxn;
int main(){
  	int t;
  	cin>>t;
  	for(int j=0;j<t;j++){
  		maxn=0;
  		cin>>n;
  		for(int i=1;i<=n;i++)
		cin>>a[i];
  		for(int i=n;i>0;i--){
  		    if(i+a[i]<=n)
			a[i]+=a[i+a[i]];
  			if(a[i]>maxn) 
			maxn=a[i];
  		}
  	cout<<maxn<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences